package com.shenqi.parking.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.sun.security.auth.UserPrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shenqi.parking.mapper.ParkingApplyMapper;
import com.shenqi.parking.domain.ParkingApply;
import com.shenqi.parking.service.IParkingApplyService;

/**
 * 租赁意向Service业务层处理
 * 
 * @author shenqi
 * @date 2025-07-27
 */
@Service
public class ParkingApplyServiceImpl implements IParkingApplyService 
{
    @Autowired
    private ParkingApplyMapper parkingApplyMapper;


    @Override
    public List<ParkingApply> selectParkingReceivedApplyList(String creatorUsername) {
        return parkingApplyMapper.selectParkingReceivedApplyList(creatorUsername);
    }

    /**
     * 查询租赁意向
     * 
     * @param id 租赁意向主键
     * @return 租赁意向
     */
    @Override
    public ParkingApply selectParkingApplyById(Long id)
    {
        return parkingApplyMapper.selectParkingApplyById(id);
    }

    /**
     * 查询租赁意向列表
     * 
     * @param parkingApply 租赁意向
     * @return 租赁意向
     */
    @Override
    public List<ParkingApply> selectParkingApplyList(ParkingApply parkingApply)
    {
        return parkingApplyMapper.selectParkingApplyList(parkingApply);
    }

    /**
     * 新增租赁意向
     * 
     * @param parkingApply 租赁意向
     * @return 结果
     */
    @Override
    public int insertParkingApply(ParkingApply parkingApply)
    {
        parkingApply.setCreateTime(DateUtils.getNowDate());
        return parkingApplyMapper.insertParkingApply(parkingApply);
    }

    /**
     * 修改租赁意向
     * 
     * @param parkingApply 租赁意向
     * @return 结果
     */
    @Override
    public int updateParkingApply(ParkingApply parkingApply)
    {
        parkingApply.setUpdateTime(DateUtils.getNowDate());
        return parkingApplyMapper.updateParkingApply(parkingApply);
    }

    /**
     * 批量删除租赁意向
     * 
     * @param ids 需要删除的租赁意向主键
     * @return 结果
     */
    @Override
    public int deleteParkingApplyByIds(Long[] ids)
    {
        return parkingApplyMapper.deleteParkingApplyByIds(ids);
    }

    /**
     * 删除租赁意向信息
     * 
     * @param id 租赁意向主键
     * @return 结果
     */
    @Override
    public int deleteParkingApplyById(Long id)
    {
        return parkingApplyMapper.deleteParkingApplyById(id);
    }

    /**
     * 删除申请，管理员可以随便删除，但是用户只能删除自己的。
     * @param ids 意向ids
     * @return
     */
    @Override
    public int deleteMyParkingApplyByIds(Long[] ids) {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            int rows = parkingApplyMapper.deleteParkingApplyByIds(ids);
            return rows;
        }
        for (Long id: ids) {
            boolean exist = parkingApplyMapper.existsByIdAndOwner(id, SecurityUtils.getUsername());
            if(!exist) {
                throw new ServiceException("不存在相关申请");
            }
        }
        int rows = parkingApplyMapper.deleteParkingApplyByIds(ids);
        return rows;
    }
}
